package com.fanbai.score.common.activity;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.business.activity.BusinessMainActivity;
import com.fanbai.score.business.http.Busin_User_RequestNet;
import com.fanbai.score.common.Manager.ActivityManager;
import com.fanbai.score.common.Utils.AppUtils;
import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.Utils.SPUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.Utils.pop.PopListWin;
import com.fanbai.score.common.base.BaseActivity;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.common.view.CircleImageView;
import com.fanbai.score.manage.activity.ManageActivity;
import com.fanbai.score.staff.activity.StaffMainActivity;
import com.fanbai.score.user.bean.User_Bean;
import com.fanbai.score.user.activity.UserMainActivity;
import com.fanbai.score.user.http.UserRequestNetwork_Utils;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by acer on 2017/5/7.
 */

public class LoginActivity extends BaseActivity{

    private PopListWin popListWin;

    @BindView(R.id.activityLogin_logo)
    CircleImageView userIcon;
    @BindView(R.id.activityLogin_role)
    TextView roleTextView;
    @BindView(R.id.activityLogin_userName)
    EditText userNameEdit;
    @BindView(R.id.activityLogin_password)
    EditText passWordEdit;
    @BindView(R.id.activityLogin_password_switch)
    ImageButton swithcImg;
    @BindView(R.id.layout_content)
    LinearLayout layoutContent;
    @BindView(R.id.logint_loginBut)
    Button loginBut;

    @OnClick(R.id.activityLogin_role)
    public void onClickRole(){
        if (popListWin == null){
            popListWin = new PopListWin(ctx);
        }
        popListWin.setListData( Arrays.asList(AppUtils.getResource().getStringArray(R.array.role_user)));
        popListWin.setOnSexClickListener(new PopListWin.OnSexClickListener() {
            @Override
            public void OnSexClickListener(boolean b, String sex, int index) {
                if(b){
                    roleTextView.setText(sex);
                }
            }
        });
        popListWin.show(roleTextView);
    }
    /**
     * 默认隐藏密码输入
     */
    private boolean isShowPw = false;
    @OnClick(R.id.activityLogin_password_switch)
    public void onclickSwitchImg(){
        isShowPw = !isShowPw;
        if(isShowPw){
            swithcImg.setImageResource(R.mipmap.icon_pw_on);
            passWordEdit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//显示密码
        }else{
            swithcImg.setImageResource(R.mipmap.icon_pw_off);
            passWordEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());//隐藏密码
        }
    }
    @OnClick(R.id.logint_loginBut)
    public void loginClick(){
        String role = roleTextView.getText().toString().trim();
        if(role == null || role.equals("")){
            ToastUtils.WarnImageToast(ctx, "请选择登录角色");
            return ;
        }
        String userName = userNameEdit.getText().toString().trim();
        if(TextUtils.isEmpty(userName)){
            ToastUtils.WarnImageToast(ctx, "手机号不可为空");
            return;
        }
        /*if(!StringUtils.checkPhoneNumber(userName)){
            ToastUtils.WarnImageToast(ctx, "手机号格式错误");
            return;
        }*/
        String passWord = passWordEdit.getText().toString().trim();
        if(TextUtils.isEmpty(passWord)){
            ToastUtils.WarnImageToast(ctx, "密码不可为空");
            return;
        }
        if(role.equals("用户")){
            requestUserLogin(userName, passWord);
        }else if(role.equals("店员")){
            jumpToActivity(StaffMainActivity.class);
        }else if(role.equals("商户")){
//            jumpToActivity(BusinessMainActivity.class);
            requestBusinessLogin(userName, passWord);
        }else if(role.equals("股东")){
            jumpToActivity(ManageActivity.class);
        }else{
            ToastUtils.WarnImageToast(ctx, "登录角色不合法");
            return ;
        }
    }
    @OnClick(R.id.logint_registerBut)
    public void registerClick(){
        jumpToActivity(Rreg_Activity.class);
    }

    @OnClick(R.id.activityLogin_forgetPassWord)
    public void onForgetPassWord(){
        jumpToActivity(PWReset_Activity.class);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_login;
    }

    @Override
    public String getTitleName() {
        return "登录";
    }

    @Override
    public int getTitleLeftName() {
        return R.mipmap.back_click;
    }

    @Override
    public void onClickTitleLeft() {
        ActivityManager.getInstance().finishAllActivity();
    }

    @Override
    public int getTitleBarColor() {
        return R.color.colorPrimary;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    private void nextAct(){
        String role = roleTextView.getText().toString().trim();
        if(role.equals("用户")){
            jumpToActivity(UserMainActivity.class);
        }else if(role.equals("店员")){
            jumpToActivity(StaffMainActivity.class);
        }else if(role.equals("商户")){
            jumpToActivity(BusinessMainActivity.class);
        }else if(role.equals("股东")){
            jumpToActivity(ManageActivity.class);
        }else{
            ToastUtils.WarnImageToast(ctx, "登录角色不合法");
            return ;
        }
    }

    private void requestUserLogin(String mobile, String pw){
        showDialog();
        UserRequestNetwork_Utils.login(ctx, mobile, pw, new CallBack<User_Bean>() {
            @Override
            public void onSuccess(String code, String msg, User_Bean bean) {
                SPUtils.with(ctx).save(SPUtils.KEY_USER_USERINFO, JsonUtils.toJsonObj(bean));
                hideDialog();
                nextAct();
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                hideDialog();
                ToastUtils.WarnImageToast(ctx, ""+msg);
            }

            @Override
            public void onError(String code, String msg) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, ""+msg);
            }
        });
    }
    private void requestStaffLogin(){

    }
    private void requestBusinessLogin(String mobile, String pw){
        showDialog();
        Busin_User_RequestNet.login(ctx, mobile, pw, new CallBack<User_Bean>() {
            @Override
            public void onSuccess(String code, String msg, User_Bean bean) {
                SPUtils.with(ctx).save(SPUtils.KEY_BUSINESS_USERINFO, JsonUtils.toJsonObj(bean));
                hideDialog();
                nextAct();
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                hideDialog();
                ToastUtils.WarnImageToast(ctx, ""+msg);
            }

            @Override
            public void onError(String code, String msg) {
                hideDialog();
                ToastUtils.ErrorImageToast(ctx, ""+msg);
            }
        });
    }
    private void requestManageLogin(){

    }
}
